//
//  TopUpViewController.m
//  FlowerShop
//
//  Created by appleLJ on 2017/8/14.
//  Copyright © 2017年 appleLJ. All rights reserved.
//

#import "TopUpViewController.h"
//#import <AlipaySDK/AlipaySDK.h>

@interface TopUpViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIView *headerView;
@property(nonatomic,strong) UIView *footerView;
@property(nonatomic,strong) NSArray *dataArray;

@property(nonatomic,strong) UIButton *weixinButton;
@property(nonatomic,strong) UIButton *zhifubaoButton;
@property(nonatomic,assign) int type; //支付方式：0微信，1支付宝
@property(nonatomic,strong) UITextField *moneyTextField;

- (void)payByALiWithHandle:(NSString *)orderString;
- (void)weiXinpay:(NSDictionary *)dic;
@end
/**
 支付宝调用地址：http://192.168.1.138:9510/ExoticFlowers/pay/toPay.action    请求参数：money：金额   user_id：用户id
 http://119.23.251.22/ExoticFlowers/pay/toPay.action
 */


@implementation TopUpViewController(NetWorking)
//- (void)aliPayForRechare{
////    NSNumber *num = [NSNumber numberWithInteger:self.moneyTextField.text.integerValue];
//    NSNumber *num = [NSNumber numberWithFloat:self.moneyTextField.text.floatValue];
//    NSDictionary *parameter = @{@"money":num,@"user_id":[UserModel sharedInstanced].userId};
//    [[AFAppDotNetAPIClient sharedClient]POST:@"pay/toPay.action" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
//        [self payByALiWithHandle:responseObject[@"alSign"]];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self showError:error];
//    }];
//}
//- (void)wxPayForRechare{
//    //    NSNumber *num = [NSNumber numberWithInteger:self.moneyTextField.text.integerValue];
//    NSNumber *num = [NSNumber numberWithFloat:self.moneyTextField.text.floatValue];
//    NSDictionary *parameter = @{@"money":self.moneyTextField.text,@"user_id":[UserModel sharedInstanced].userId};
//    [[AFAppDotNetAPIClient sharedClient]POST:@"pay/toPay_Weixin.action" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
//        [self weiXinpay:responseObject];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self showError:error];
//    }];
//}
@end
@implementation TopUpViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"充值";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.type = 0; //微信支付
    self.weixinButton.hidden = YES;
    self.zhifubaoButton.hidden = YES;
}
#pragma mark - event
//微信
- (void)weixinAction{
    self.type = 0;
    [self.weixinButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanzhong"] forState:UIControlStateNormal];
    [self.zhifubaoButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanze"] forState:UIControlStateNormal];
}

//支付宝
- (void)zhifubaoAction{
    self.type = 1;
    [self.weixinButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanze"] forState:UIControlStateNormal];
    [self.zhifubaoButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanzhong"] forState:UIControlStateNormal];
}

//充值
- (void)topupAction{
    if (self.type == 0) {
        NSLog(@"微信支付");
        if (self.moneyTextField.text.length <= 0) {
            ShowMessage(@"请输入充值金额");
            return;
        }
//        [self wxPayForRechare];
    }else{
//        NSLog(@"支付宝支付");
//        if (self.moneyTextField.text.length <= 0) {
//            ShowMessage(@"请输入充值金额");
//            return;
//        }
//        //1、 调用服务器接口
//        [self aliPayForRechare];
//        //2、 调用服务器接口 返回成功后条用支付方法payByALiWithHandle
    }
}

/**
 支付宝支付
 
 @param orderString orderString description
 参数：orderString 后台返回签名加密的 字符串
 */
//- (void)payByALiWithHandle:(NSString *)orderString{
//    //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
//    NSString *appScheme = @"alisdkFlowerShop";
//    // NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
//    // NOTE: 调用支付结果开始支付
//    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
//        NSLog(@"reslut = %@",resultDic);
//
//        if ([resultDic[@"resultStatus"] isEqualToString:@"6001"]) {
//            ShowMessage(resultDic[@"memo"]);
//        }
//        if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
//            [self.view makeToast:@"支付成功!" duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
//                [self.navigationController popViewControllerAnimated:YES];
//            }];
//        }
//    }];
//}

/**
 微信支付
 */
//- (void)weiXinpay:(NSDictionary *)dic{
//    PayReq *request = [[PayReq alloc]init];
//    request.partnerId = dic[@"partnerid"];
//    request.prepayId= dic[@"prepayid"];
//    request.package = dic[@"package"];
//    request.nonceStr= dic[@"noncestr"];
//    request.timeStamp= [dic[@"timestamp"] intValue];
//    request.sign= dic[@"sign"];
//    [WXApi sendReq:request];
//}

#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    return cell;
}

#pragma mark -- init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = getColor(bgColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        _headerView.backgroundColor = getColor(whiteColor);
        
        //充值金额文字
        UILabel *balanceLabel = [[UILabel alloc] init];
        balanceLabel.font = DEF_FontSize_15;
        balanceLabel.textColor = getColor(mainColor);
        balanceLabel.textAlignment = NSTextAlignmentCenter;
        balanceLabel.text = @"充值金额";
        
        //输入框
        UITextField *textField = [[UITextField alloc] init];
        textField.font = DEF_FontSize_15;
        textField.textColor = getColor(mainColor);
        textField.textAlignment = NSTextAlignmentCenter;
        //[textField setValue:[NSNumber numberWithInt:5] forKey:@"paddingRight"];
        //textField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 10)];
        textField.layer.borderColor = [getColor(mainColor) CGColor];
        textField.layer.borderWidth = 0.5;
        textField.layer.cornerRadius = 5;
        textField.layer.masksToBounds = YES;
        textField.text = @"";
        self.moneyTextField = textField;
        
        //单位
        UILabel *unitLabel = [[UILabel alloc] init];
        unitLabel.font = DEF_FontSize_15;
        unitLabel.textColor = getColor(mainColor);
        unitLabel.textAlignment = NSTextAlignmentCenter;
        unitLabel.text = @"¥";
        
        //微信
        UIButton *weixinButton = [[UIButton alloc] init];
        weixinButton.titleLabel.font = DEF_FontSize_14;
        [weixinButton setTitle:@"微信" forState:UIControlStateNormal];
        [weixinButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [weixinButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanzhong"] forState:UIControlStateNormal];
        [weixinButton addTarget:self action:@selector(weixinAction) forControlEvents:UIControlEventTouchUpInside];
        self.weixinButton = weixinButton;
        
        //支付宝
        UIButton *zhifubaoButton = [[UIButton alloc] init];
        zhifubaoButton.titleLabel.font = DEF_FontSize_14;
        [zhifubaoButton setTitle:@"支付宝" forState:UIControlStateNormal];
        [zhifubaoButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [zhifubaoButton setImage:[UIImage imageNamed:@"user_chongzhi_xuanze"] forState:UIControlStateNormal];
        [zhifubaoButton addTarget:self action:@selector(zhifubaoAction) forControlEvents:UIControlEventTouchUpInside];
        self.zhifubaoButton = zhifubaoButton;
        
        //充值按钮
        UIButton *topUpButton = [[UIButton alloc] init];
        topUpButton.titleLabel.font = DEF_FontSize_16;
        [topUpButton setTitle:@"充值" forState:UIControlStateNormal];
        [topUpButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [topUpButton setBackgroundColor:getColor(mainColor)];
        [topUpButton addTarget:self action:@selector(topupAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        //添加控件
        [_headerView addSubview:balanceLabel];
        [_headerView addSubview:textField];
        [_headerView addSubview:unitLabel];
        [_headerView addSubview:weixinButton];
        [_headerView addSubview:zhifubaoButton];
        [_headerView addSubview:topUpButton];
        
        //约束
        [balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headerView).offset(150 / WIDTH_5S_SCALE);
            make.left.equalTo(_headerView).offset(30 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(80);
        }];
        [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_headerView).offset(-30 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(20);
            make.centerY.equalTo(balanceLabel);
        }];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(balanceLabel.mas_right).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(unitLabel.mas_left).offset(-5 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(30);
            make.centerY.equalTo(balanceLabel);
        }];
        [weixinButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headerView).offset(60 / WIDTH_5S_SCALE);
            make.top.equalTo(balanceLabel.mas_bottom).offset(40 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(20);
        }];
        [zhifubaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weixinButton.mas_right).offset(50 / WIDTH_5S_SCALE);
            make.centerY.equalTo(weixinButton);
            make.width.height.equalTo(weixinButton);
        }];
        [topUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weixinButton.mas_bottom).offset(30 / WIDTH_5S_SCALE);
            make.left.equalTo(_headerView.mas_left).offset(30 / WIDTH_5S_SCALE);
            make.right.equalTo(_headerView.mas_right).offset(-30 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(40);
            topUpButton.layer.cornerRadius = 5;
            topUpButton.layer.masksToBounds = YES;
        }];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}


#pragma mark -- data

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}


@end
